package com.hgg.utils;

import com.google.gson.Gson;
import com.hgg.utils.JsonUtils;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class WebJsonUtil {

    private static final OkHttpClient client = new OkHttpClient();
    private static Map<String, String> map = new HashMap<>();


    public static List getListMap() throws Exception {
        //setURLMap("20k",2);
        Request request = new Request.Builder()
                .url("https://pub.alimama.com" + map.get("path"))
                .header("user_agent", map.get("user_agent"))
                .header("cookie", map.get("cookie"))
                .header("referer", map.get("referer"))
                .header("accept", map.get("accept"))
                .header("path", map.get("path"))
                .header("x-requested-with", map.get("x_requested_with"))
                .build();
        Response response = client.newCall(request).execute();
        /*System.out.println(response);
        System.out.println(response.body().byteStream());*/
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

        List<Map<String, Object>> list = JsonUtils.parserJson(response.body().byteStream(), "data", "pageList");
     /*  for (Map<String,Object> map:list) {
            for (Map.Entry<String,Object> entry:map.entrySet()) {
                System.out.println(entry.getKey()+"----"+entry.getValue());
            }
        }*/

        return list;
    }
    public static  void setURLMap(String channel,int catIds){
        map.put("user_agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
        map.put("cookie", "t=d50b7a8bee05bc8ada989dc032392fbf; cna=Y1f/Etrj03sCAWcZHB7A1Tgk; _umdata=486B7B12C6AA95F22A1024BEEA3A740B6735DFCE09AFB7AC53517CD3BC2714ECF4C05002C6A3C191CD43AD3E795C914CA7DE1EDA54E542D83F54E5284CA86799; cookie2=10ff126377d5a72191af84a85b760be2; _tb_token_=5bee3efe03d9a; v=0; isg=BPz8CZVxiy-Ojb_EbjakHfKlzZpuXaEBwzpK_NZ9COfKoZwr_gVwr3IXhYF809h3");
        map.put("referer", "https://pub.alimama.com/promo/item/channel/index.htm?spm=a219t.7900221%2F1.1998910419.d5f9e8538.2a8f75a5lDabcJ&channel="+channel+"&toPage=1");
        map.put("accept", "application/json, text/javascript, */*; q=0.01");
        map.put("path", "/items/channel/"+channel+".json?spm=a219t.7900221/1.1998910419.d5f9e8538.2a8f75a58K2xEY&channel="+channel+"&toPage=1&catIds="+catIds+"&level=1&perPageSize=50&shopTag=&t=1537235972502&_tb_token_=5bee3efe03d9a&pvid=16_219.143.130.241_3517_1537235971937");
        map.put("x_requested_with", "XMLHttpRequest");
    }

    public static Map geturlmap() {
        //setURLMap("20k",2);
        Map<String, String> urlmap = new HashMap<>();
        String[] paths = map.get("path").split("&");
        for (int i = 0; i < paths.length; i++) {
          System.out.println(paths[i]);
          if(paths[i].matches("(.*)channel(.*)")){
              urlmap.put("channel",paths[i].split("=")[1]);
              System.out.println(urlmap.get("channel"));
          }
          if(paths[i].matches("(.*)catIds(.*)")){
              urlmap.put("catIds",paths[i].split("=")[1]);
              System.out.println(urlmap.get("catIds"));
          }
        }
        return urlmap;
    }


    public static void main(String[] args) throws Exception {
        //getListMap();
        //geturlmap();
    }

}
